package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.ImGroupRuleDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.ImGroupRule;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.AdminImGroupRuleAddDTO;
import com.sc.nft.entity.dto.AdminImGroupRuleUpdateDTO;
import com.sc.nft.entity.vo.AdminGroupRuleVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ImGroupRuleService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class ImGroupRuleServiceImpl implements ImGroupRuleService {

    private final ImGroupRuleDao imGroupRuleDao;
    private final UserInfoDao userInfoDao;

    @Override
    public List<ImGroupRule> getAll() {
        return imGroupRuleDao.getAll();
    }

    /**
     * 后台获取配置规则列表
     *
     * @param pageRequest
     * @return
     */
    @Override
    public Page<AdminGroupRuleVO> getList(PageRequest pageRequest) {
        return imGroupRuleDao.getList(pageRequest);
    }

    @Override
    public void add(AdminImGroupRuleAddDTO adminImGroupRuleAddDTO) {
        UserInfo oneByUserTel = userInfoDao.getOneByUserTel(adminImGroupRuleAddDTO.getUserTel());
        if (BeanUtil.isEmpty(oneByUserTel)) {
            throw new GlobalRunTimeException("用户不存在");
        }
        Boolean intersectionExamine = intersectionExamine(null, adminImGroupRuleAddDTO.getStart(), adminImGroupRuleAddDTO.getEnd());
        if (intersectionExamine) {
            throw new GlobalRunTimeException("数据有交集,新增失败");
        }
        ImGroupRule imGroupRule = new ImGroupRule();
        BeanUtil.copyProperties(adminImGroupRuleAddDTO, imGroupRule);
        imGroupRule.insert();
    }

    @Override
    public void update(AdminImGroupRuleUpdateDTO adminImGroupRuleAddDTO) {
        UserInfo oneByUserTel = userInfoDao.getOneByUserTel(adminImGroupRuleAddDTO.getUserTel());
        if (BeanUtil.isEmpty(oneByUserTel)) {
            throw new GlobalRunTimeException("用户不存在");
        }
        Boolean intersectionExamine = intersectionExamine(adminImGroupRuleAddDTO.getId(), adminImGroupRuleAddDTO.getStart(), adminImGroupRuleAddDTO.getEnd());
        if (intersectionExamine) {
            throw new GlobalRunTimeException("数据有交集,修改失败");
        }
        ImGroupRule imGroupRule = new ImGroupRule();
        BeanUtil.copyProperties(adminImGroupRuleAddDTO, imGroupRule);
        imGroupRule.updateById();
    }

    @Override
    public Boolean intersectionExamine(Long id, Integer start, Integer end) {
        Integer sum = imGroupRuleDao.intersectionExamine(id, start, end);
        return sum >= 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * 根据官方群数量获取对应的用户
     *
     * @param sum
     * @return
     */
    @Override
    public ImGroupRule accordingOfficialGroupSumGetRule(Integer sum) {
        return imGroupRuleDao.accordingOfficialGroupSumGetRule(sum);
    }

}
